package org.wattdepot.gdata.kukuicup.updater.energyminiview;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.gdata.kukuicup.service.GDataService;
import org.wattdepot.gdata.kukuicup.service.WattDepotService;
import org.wattdepot.gdata.kukuicup.updater.GDataTask;
//import org.wattdepot.gdata.kukuicup.updater.RollingData;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Implements the task to obtain data on power levels every few seconds for the past few minutes.
 * @author Philip Johnson
 */
public class EnergyMiniViewTask extends GDataTask {
  
  /** Round 1 start. */
  //private RollingData rollingData = new RollingData(10);
  
  /** Holds the round 1-3 start and end dates. */
  private Map<String, XMLGregorianCalendar> roundDates = new HashMap<String,XMLGregorianCalendar>();
  
  /** Holds the various date keys. */
  private String[] dateIDs = {"round1.start", "round1.end", "round2.start", "round2.end", 
      "round3.start", "round3.end"};

  
  /**
   * Creates the task that gets woken up regularly (typically every 15 seconds). 
   * @param wattDepotService The WattDepot service configuration data. 
   * @param gdataService The gdata service configuration data.
   * @param spreadsheetURL The URL to the spreadsheet containing the data.
   * @param sourceName The WattDepot source of interest.
   * @param isVirtual True if the source is virtual.
   * @param row The row in the spreadsheet that this source's data appears in.
   */
  public EnergyMiniViewTask(WattDepotService wattDepotService, GDataService gdataService, 
      URL spreadsheetURL, String sourceName, boolean isVirtual, int row) {
    super(wattDepotService, gdataService, spreadsheetURL, sourceName, row, "EnergyMiniView",
        isVirtual);
    System.out.println("Setting up EnergyMiniView task.");
    for (String dateID : dateIDs) {
      initializeDate(dateID);
    }
    
  }

  /**
   * Updates the roundDates instance data structure with the XMLGregorianCalendar 
   * instance corresponding to the property file value.
   * The hrs, mins, secs, millis fields are initialized to zero (the start of the day). 
   * @param dateID A string such as "round1.start", "round1.end", etc. This becomes
   * the key in the roundDates data structure for the corresponding XMLGregorianCalendar
   * instance. 
   */
  private void initializeDate(String dateID) {
    String propertyKey = "energy.miniview." + dateID;
    String dateString = wattDepotService.getProperties().getRequiredProperty(propertyKey);
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
    XMLGregorianCalendar date;
    try {
      date = Tstamp.makeTimestamp(dateFormat.parse(dateString).getTime());
      date.setHour(0);
      date.setMinute(0);
      date.setSecond(0);
      date.setMillisecond(0);
    }
    catch (Exception e) {
      throw new RuntimeException("Error parsing energy miniview date: " + dateString, e);
    }
    roundDates.put(dateID, date);
  }

  /**
   * Retrieve the power data from WattDepot and place it into the GData spreadsheet.
   */
  @Override
  public void run() {
    System.out.println("Running miniview.");
  
  }
}

